import { Component, OnInit } from '@angular/core';
import { AppPaginationComponent, PaginationModel } from "../../../widgets/pagination/pagination.component";
import { EnclosureService } from './enclosure.service';
import { Util } from 'app/common/util';
import * as _ from 'lodash';
import { DataService } from '../../../services/data.service';
@Component({
  selector: 'app-enclosure',
  templateUrl: './enclosure.component.html',
  styleUrls: ['./enclosure.component.scss'],
  providers: [EnclosureService]
})
export class EnclosureComponent implements OnInit {
  // lodash
  public _: any = _;

  //综合查询
  // 列表是否全选
  public isCheckedAllComprehensive = false;
  // 养老基金附件类型查询条件
  public oldageAttachConfComprehensive: any;

  // 菜单数据
  public menuData: any;
  // 菜单权限set
  public menuResourceSet: any = [];

  //从字典值获取综合查询的受托人下拉框选项
  public oldageLinkPrincipalComprehensive: any = {
    LINK_PRINCIPAL_NO: []
  };

  // 综合查询分页列表数据
  public dataListComprehensive = [];
  // 查询分页列表post数据
  public getBody: any = {
    pageIndex: 1,
    pageSize: 10,
    sFundType: 4,
    sFundCode: '',
    sFullName: '',
    sAttachName: ''
    // sAttachParentCode: '',
    // sAttachCode: '',
    // sLinkPrincipalNo: '',
  };
  public postBody: any = {
    pageIndex: 1,
    pageSize: 10,
    sFundType: 4,
    sFundCode: '', // 组合代码
    sFullName: '', // 组合名称
    sAttachName: '', //文件名称
    sAttachType: '', // 附件类型
    sCreator: '', //上传人
    sLinkPrincipal: '', //受托人
    sAttachDesc: '', // 附件说明
    updateLoadDate: '', // 上传时间
    updateLoadEnd: '', // 上传时间
  }
  // 是否为模态框查询
  public isQuery: any = false;
  // 备案机构、受托人、托管人
  public distionary: any = {
    RECORDORG_TYPE: [],  // 备案机构
    LINK_PRINCIPAL_NO: [],   //受托人
    TRUSTEE: []   //  托管人
  }
  // 字典值
  public dictionaryList: any = {
    // LINK_PRINCIPAL_NO: []
  }
  //综合查询分页配置
  public listPageInfoComprehensive: PaginationModel = {
    currentPageNum: 1,
    pageSize: 10,
    totalPages: 1,
    total: 0,
    pagesShow: 5,
    startRow: 0,
    endRow: 0,
    pageList: [5, 10, 25, 50, 100]
  };
  // 综合查询/受托人附件-的附件类型查询条件获取
  getAttachConfComprehensive() {
    let that = this;
    this.enclosureService.getAttachConf().subscribe(data => {
      that.oldageAttachConfComprehensive = _.clone(data[0].childrenAttachType);
      that.oldageAttachConf = _.filter(data[0].childrenAttachType, function (item: any) {
        return item.sAttachTypeName != '关联人员';
      })
      // that.oldageAttachConf = _.clone(data[0].childrenAttachType);
    })
  }
  //综合查询列表是否全选
  checkedAllComprehensive() {
    if (this.isCheckedAllComprehensive) { // 更新为全选
      _.forEach(this.dataListComprehensive, item => {
        item.isChecked = true;
      });
    } else { // 更新为不全选
      _.forEach(this.dataListComprehensive, item => {
        item.isChecked = false;
      });
    }
  }
  // 综合查询多个单选后当前页列表是否全选
  ctrlCheckAllComprehensive() {
    let that = this;
    let checkList = [];
    _.forEach(this.dataListComprehensive, item => {
      if (item.isChecked == true) {
        checkList.push(item);
      }
    })
    if (checkList.length == this.dataListComprehensive.length) {
      this.isCheckedAllComprehensive = true;
    } else {
      this.isCheckedAllComprehensive = false;
    }
  }
  ngOnInit() {
    let that = this;
    this.listComprehensive();
    this.getAttachConfComprehensive();
    // 权限处理
    this.dataService.initTree.subscribe(data => {
      if (data) {
        let SessionMenuData = sessionStorage.getItem('menu');
        this.menuData = JSON.parse(SessionMenuData);
        this.menuResourceSet = [];
        _.forEach(this.menuData, (item, index) => {
          if (item.sName == '企业年金') {
            _.forEach(item.children, (it, idx) => {
              if (it.sName == '附件查询') {
                that.menuResourceSet = it.resourceSet;
                sessionStorage.setItem('btn-an', this.menuResourceSet);
              }
            })
          }
        })
      }
    })
    let SessionMenuData = sessionStorage.getItem('menu');
    this.menuData = JSON.parse(SessionMenuData);
    this.menuResourceSet = [];
    _.forEach(this.menuData, (item, index) => {
      if (item.sName == '企业年金') {
        _.forEach(item.children, (it, idx) => {
          if (it.sName == '附件查询') {
            that.menuResourceSet = it.resourceSet;
            sessionStorage.setItem('btn-an', this.menuResourceSet);
          }
        })
      }
    })
    // 综合查询通过服务获取字典值
    this.oldageLinkPrincipalComprehensive = JSON.parse(localStorage.getItem("dictionaryList"));
    // 受托人附件通过服务获取字典值
    this.oldageLinkPrincipal = JSON.parse(localStorage.getItem("dictionaryList"));
    this.dictionaryList = JSON.parse(localStorage.getItem("dictionaryList"));
    this.getDictionary();
  }
  ngAfterViewInit() {
    let that = this;
    setTimeout(function () {
      // 受托人
      window['$']('#sLinkPrincipalNoSelected').select2({ width: '190px' });
      window['$']('#sLinkPrincipalNoSelected').on("select2:select", function (e) {
        that.postBody.sLinkPrincipal = window['$']('#sLinkPrincipalNoSelected').select2("val");
      })
      window['$']('#sLinkPrincipalNoSelecteds').select2({ width: '190px' });
      window['$']('#sLinkPrincipalNoSelecteds').on("select2:select", function (e) {
        that.getListPost.sLinkPrincipalNo = window['$']('#sLinkPrincipalNoSelecteds').select2("val");
      })
      // 附件类型
      window['$']('#sProductTypeSelected').select2({ width: '190px' });
      window['$']('#sProductTypeSelected').on("select2:select", function (e) {
        that.postBody.sAttachType = window['$']('#sProductTypeSelected').select2("val");
      })
      window['$']('#sProductTypeSelecteds').select2({ width: '190px' });
      window['$']('#sProductTypeSelecteds').on("select2:select", function (e) {
        that.getListPost.sAttachCode = window['$']('#sProductTypeSelecteds').select2("val");
      })
    }, 100);
  }
  // 查询判断
  loadcheck(type: any) {
    if (type == 'page') {
      this.isQuery = false;
      this.listComprehensive(1);
    } else if (type == 'query') {
      this.isQuery = true;
      this.listComprehensive(1);
    }
  }
  // 综合查询分页列表
  listComprehensive(pageNum?, pageSize?) {
    let that = this;
    this.isCheckedAllComprehensive = false;
    let body: any;
    // 判断是页面查询还是搜索框查询
    if (this.isQuery) {
      // 清空页面查询条件
      this.getBody.sFullName = '';
      this.getBody.sFundCode = '';
      this.getBody.sAttachName = '';
      // 去空格
      _.forIn(this.postBody, (value, key) => {
        if (key == 'pageSize' || key == 'pageIndex' || key == 'sFundType') {
          // 这里不做处理，排除number类型
        } else {
          that.postBody[key] = _.trim(that.postBody[key]);
        }
      })
      this.postBody.pageIndex = pageNum ? pageNum : this.listPageInfoComprehensive.currentPageNum;
      this.postBody.pageSize = pageSize ? pageSize : this.listPageInfoComprehensive.pageSize
      body = _.cloneDeep(this.postBody);
    } else {
      // 去空格
      this.getBody.sFullName = _.trim(this.getBody.sFullName);
      this.getBody.sFundCode = _.trim(this.getBody.sFundCode);
      this.getBody.sAttachName = _.trim(this.getBody.sAttachName);
      this.getBody.pageIndex = pageNum ? pageNum : this.listPageInfoComprehensive.currentPageNum;
      this.getBody.pageSize = pageSize ? pageSize : this.listPageInfoComprehensive.pageSize
      body = _.cloneDeep(this.getBody);
    }
    this.enclosureService.listComprehensive(body).subscribe(data => {
      if (data) {
        that.dataListComprehensive = data.list;
        that.listPageInfoComprehensive.currentPageNum = data.pageNum;
        that.listPageInfoComprehensive.totalPages = data.pages;
        that.listPageInfoComprehensive.total = data.total;
        that.listPageInfoComprehensive.startRow = data.startRow;
        that.listPageInfoComprehensive.endRow = data.endRow;
        window['$']('#annuityEnclosureQuery').modal('hide');
      }
    });
  }
  // 综合查询按选取个数分页
  listpageSizeChangeComprehensive(pageSize: number) {
    if (pageSize !== this.listPageInfoComprehensive.pageSize) {
      this.listPageInfoComprehensive.pageSize = pageSize;
      this.listComprehensive();
    }
  }
  // 综合查询页码分页导航
  listPageNavigationComprehensive(currentPageNum: number) {
    this.listPageInfoComprehensive.currentPageNum = currentPageNum;
    this.listComprehensive(this.listPageInfoComprehensive.currentPageNum, this.listPageInfoComprehensive.pageSize);
  }
  // 综合查询批量下载附件
  attachDownloadComprehensive() {
    // 获取下载附件的内码
    let checkedItem = _.concat([], _.map(_.filter(this.dataListComprehensive, { isChecked: true }), "nAttachNo"));
    if (checkedItem.length == 0) {
      window['swal']("提示", "请选择附件再下载 !", "warning")
    } else {
      // 下载附件
      Util.attachDownload(checkedItem);
    }
  }

  //受托人附件查询
  // 受托人附件列表是否全选
  public isCheckedAll = false;
  //从字典值获取受托人附件的受托人下拉框选项
  public oldageLinkPrincipal: any = {
    LINK_PRINCIPAL_NO: [],
    ANNUITY_TYPE: []
  };
  // 受托人附件-附件类型查询条件
  public oldageAttachConf: any;
  // 受托人附件查询分页列表
  public dataList = [];
  // 受托人附件分页列表post数据
  public getListPost: any = {
    pageIndex: 1,
    pageSize: 10,
    sFundType: 4,
    sAttachName: '',
    sAttachCode: '',
    sLinkPrincipalNo: '',
  };
  //受托人附件分页配置
  public listPageInfo: PaginationModel = {
    currentPageNum: 1,
    pageSize: 10,
    totalPages: 1,
    total: 0,
    pagesShow: 5,
    startRow: 0,
    endRow: 0,
    pageList: [5, 10, 25, 50, 100]
  };
  constructor(
    public enclosureService: EnclosureService,
    public dataService: DataService
  ) { }

  // 受托人附件列表全选
  checkedAll() {
    if (this.isCheckedAll) { // 更新为全选
      _.forEach(this.dataList, item => {
        item.isChecked = true;
      });
    } else { // 更新为不全选
      _.forEach(this.dataList, item => {
        item.isChecked = false;
      });
    }
  }
  // 受托人附件列表是否全选
  ctrlCheckAll() {
    let that = this;
    let checkList = [];
    _.forEach(this.dataList, item => {
      if (item.isChecked == true) {
        checkList.push(item);
      }
    })
    if (checkList.length == this.dataList.length) {
      this.isCheckedAll = true;
    } else {
      this.isCheckedAll = false;
    }
  }
  // 受托人附件分页列表
  list(pageNum?, pageSize?) {
    let that = this;
    this.isCheckedAll = false;
    this.getListPost.attachName = _.trim(this.getListPost.attachName);
    this.getListPost.fundName = _.trim(this.getListPost.fundName);
    this.getListPost.pageIndex = pageNum ? pageNum : this.listPageInfo.currentPageNum;
    this.getListPost.pageSize = pageSize ? pageSize : this.listPageInfo.pageSize;
    this.enclosureService.list(this.getListPost).subscribe(data => {
      if (data) {
        that.dataList = data.list;
        that.listPageInfo.currentPageNum = data.pageNum;
        that.listPageInfo.totalPages = data.pages;
        that.listPageInfo.total = data.total;
        that.listPageInfo.startRow = data.startRow;
        that.listPageInfo.endRow = data.endRow;
      }
    });
  }
  // 受托人附件按选取个数分页
  listpageSizeChange(pageSize: number) {
    if (pageSize !== this.listPageInfo.pageSize) {
      this.listPageInfo.pageSize = pageSize;
      this.list();
    }
  }
  // 受托人附件页码分页导航
  listPageNavigation(currentPageNum: number) {
    this.listPageInfo.currentPageNum = currentPageNum;
    this.list(this.listPageInfo.currentPageNum, this.listPageInfo.pageSize);
  }
  // 受托人附件批量下载附件
  attachDownload() {
    // 获取下载附件的内码
    let checkedItem = _.concat([], _.map(_.filter(this.dataList, { isChecked: true }), "nAttachNo"));
    if (checkedItem.length == 0) {
      window['swal']("提示", "请选择附件再下载 !", "warning")
    } else {
      // 下载附件
      Util.attachDownload(checkedItem);
    }
  }
  // 点击表格头排序
  orderData(colName, dataList, dataListName) {
    let listDataAsc = _.orderBy(dataList, [colName], ["asc"]);
    if (JSON.stringify(dataList) == JSON.stringify(listDataAsc)) {
      listDataAsc = _.orderBy(dataList, [colName], ["desc"]);
    }
    switch (dataListName) {
      case 'dataListComprehensive':
        this.dataListComprehensive = listDataAsc;
        break;
      case 'dataList':
        this.dataList = listDataAsc;
        break;
    }
  }
  // 查询重置
  resetQuery() {
    this.postBody.sFundCode = ''; // 组合代码
    this.postBody.sFullName = ''; // 组合名称
    this.postBody.sAttachName = ''; //文件名称
    this.postBody.sAttachType = ''; // 附件类型
    this.postBody.sCreator = ''; //上传人
    this.postBody.sLinkPrincipal = ''; //受托人
    this.postBody.sAttachDesc = ''; // 附件说明
    this.postBody.updateLoadDate = ''; // 上传时间
    this.postBody.updateLoadEnd = ''; // 上传时间
    window['$']('#sLinkPrincipalNoSelected').select2("val", '--请选择--'); // 受托人
    window['$']('#sProductTypeSelected').select2("val", '--请选择--'); // 附件类型
  }
  // 显示搜索模态框
  showQuery() {
    window['$']("#annuityEnclosureQuery").modal('show');
  }
  // 获取备案机构、受托人、托管人下拉框值
  getDictionary() {
    let that = this;
    this.enclosureService.getDictionary().subscribe(data => {
      if (data) {
        that.distionary.RECORDORG_TYPE = data.RECORDORG_TYPE;
        that.distionary.LINK_PRINCIPAL_NO = data.LINK_PRINCIPAL_NO;
        that.distionary.TRUSTEE = data.TRUSTEE;
      }
    })
  }
}
